/* ************************************************************************

   Copyright:

   License:

   Authors: SALMON Charles

************************************************************************ */

qx.Class.define("bip.services.Survey",
{
  extend : bip.services.AbstractService,
  
  type: "singleton",
  
  construct : function(url, delegate)
  {
    this.base(arguments);
    this._store.bind("model", this, "survey");
  },
  
  properties : {
    survey : {
      nullable: true,
      event: "changeSurvey"
    },
    
	nQuestions: {
      check : "Integer",
      nullable : true
	},
	
	nAnswers: {
	      check : "Integer",
	      nullable : true
	},
	
	creationDate: {
	      check : "Date",
	      nullable : true
	}
  },
  
  events: {
	  "changeMetaData": "qx.event.type.Event"
  },
  

  members :
  {
    _store : null,
    
    fetchSurvey : function() {
    	this._store.setUrl(null);
    	this._store.setPostModel(qx.locale.Manager.getInstance().getLocale());
    	this.addListenerOnce("state",function(evt) {
			if (evt.getData().getState() == "ok") {
				this.__setMetaData(evt.getData().getModel());
			} else {
				this.__setMetaData(null);
			}
		});
    	this._store.setUrl("survey/fetchSurvey");
    },
    
 
    
    getSurveyTree : function() {
    	this._store.setUrl(null);
    	this._store.setUrl("survey/getSurveyTree");
    },
    
    postSurvey : function(surveyFormModel) {
    	this._store.setUrl(null);
    	var survey = this.getSurvey();
    	var currentTopicId;
    	for(var i=0;i<survey.length;i++) {
  			var model = survey.getItem(i);
  			if (model.getITopic()) {
 					currentTopicId = model.getIdTopic(); 
 				} 
 				else if (model.getIQuestion()) {
 					// Get back question value
  				var qValueGetter = "getQ_"+currentTopicId+"_"+model.getIdQuestion();
  				try {
  					var qValue = surveyFormModel[qValueGetter]();
    				model.setUserValue(qValue);
  				} catch(e) {
  					alert("Unexpected error:"+e);
  				}
 				}
 				else if (model.getISurveyMetaData()) {
 					// READ-ONLY field
 					// Otherwise => model.getUserDate().setTime(surveyFormModel.getUserDate().getTime());
 				}
  		}
    	this._store.setPostModel(survey);
       	this.addListenerOnce("state",function(evt) {
			if (evt.getData().getState() == "ok") {
				this.__setMetaData(evt.getData().getModel());
			} else {
				this.__setMetaData(null);
			}
		});
    	this._store.setUrl("survey/postSurvey");
    },
    
    __setMetaData: function(survey) {
    	if (survey != null) {
    		for(var i=0;i<survey.length;i++) {
				var model = survey.getItem(i);
				if (model.getISurveyMetaData()) {
					this.setNQuestions(model.getNumberOfQuestions());
					this.setNAnswers(model.getNumberOfAnswers());
					this.setCreationDate(new Date(model.getUserDate().getTime()));
				}
    		}
    	} else {
    		this.setNQuestions(null);
			this.setNAnswers(null);
			this.setCreationDate(null);
    	}
    	this.fireEvent("changeMetaData");
    },
    
	modelSetNAnswers: function(value,old) {
		// Update model
		var survey = this.getSurvey();
		if (survey != null) {
			for(var i=0;i<survey.length;i++) {
				var model = survey.getItem(i);
				if (model.getISurveyMetaData()) {
					model.setNumberOfAnswers(value);
					this.__setMetaData(survey);
					break;
				}
	    	}
		}
    }
  }
});